package com.ycdx.experts.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.ycdx.experts.impl.ExpertImpl;
import com.ycdx.experts.pojo.Expert;

public class ExpertAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	
	private static final long serialVersionUID = 4941436091324365145L;
	HttpServletRequest request;  
    HttpServletResponse response;
    List<Expert> experts;
    Expert expert;
    private String id;
    private String field;
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response = response;
	}
	
	public void list(){
		this.response.setContentType("text/html;charset=utf-8");  
        this.response.setCharacterEncoding("UTF-8"); 
        experts = ExpertImpl.getAllExperts();
        //System.out.println(experts.size());
        //JSONArray jsonarray = JSONArray.fromObject(experts);
	    JSONArray jsonArray=new JSONArray();  
	    for(Expert expert:experts){
	    	JSONObject json=new JSONObject();  
		    json.put("id", expert.getId());  
		    json.put("name",expert.getName());  
		    json.put("dept", expert.getDept());
		    json.put("title", expert.getTitle());
		    json.put("field", expert.getField());
		    json.put("shortDesc", expert.getShortDesc());
		    json.put("desc", expert.getDesc());
		    json.put("photoURL", expert.getPhotoURL());
		    json.put("officeNum", expert.getOfficeNum());
		    json.put("mobileNum", expert.getMobileNum());
		    jsonArray.add(json);
	    }	    
	    JSONObject alObject=new JSONObject();  
	    alObject.put("experts", jsonArray);
        try {
			response.getWriter().write(alObject.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	}
	
	public void search(){
		this.response.setContentType("text/html;charset=utf-8");  
        this.response.setCharacterEncoding("UTF-8"); 
        
        try {
        	this.request.setCharacterEncoding("UTF-8");
			experts = ExpertImpl.searchExpertByField(URLDecoder.decode(field, "utf-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//System.out.println(field);
        //JSONArray jsonarray = JSONArray.fromObject(experts);
	    JSONArray jsonArray=new JSONArray();  
	    for(Expert expert:experts){
	    	JSONObject json=new JSONObject();  
		    json.put("id", expert.getId());  
		    json.put("name",expert.getName());  
		    json.put("dept", expert.getDept());
		    json.put("title", expert.getTitle());
		    json.put("field", expert.getField());
		    json.put("shortDesc", expert.getShortDesc());
		    json.put("desc", expert.getDesc());
		    json.put("photoURL", expert.getPhotoURL());
		    json.put("officeNum", expert.getOfficeNum());
		    json.put("mobileNum", expert.getMobileNum());
		    jsonArray.add(json);
	    }	    
	    JSONObject alObject=new JSONObject();  
	    alObject.put("experts", jsonArray);
        try {
			response.getWriter().write(alObject.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	}
	
	public void getExpertById(){
		this.response.setContentType("text/html;charset=utf-8");  
        this.response.setCharacterEncoding("UTF-8"); 
        expert = ExpertImpl.getExpertById(id);
        //System.out.println(experts.size());
        //JSONArray jsonarray = JSONArray.fromObject(experts);
	    //JSONArray jsonArray=new JSONArray();  
	    //for(Expert expert:experts){
    	JSONObject json=new JSONObject();  
	    json.put("id", expert.getId());  
	    json.put("name",expert.getName());  
	    json.put("dept", expert.getDept());
	    json.put("title", expert.getTitle());
	    json.put("field", expert.getField());
	    json.put("shortDesc", expert.getShortDesc());
	    json.put("desc", expert.getDesc());
	    json.put("photoURL", expert.getPhotoURL());
	    json.put("officeNum", expert.getOfficeNum());
	    json.put("mobileNum", expert.getMobileNum());
		    //jsonArray.add(json);
	    //}	    
	    JSONObject alObject=new JSONObject();  
	    alObject.put("expert", json);
        try {
			response.getWriter().write(alObject.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}
	
}
